/* 
 * BaseDWR.java      1.00    2008-01-11
 *
 * Copyright (c) 2010 monolist. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information
 * of monolist.  You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement
 * you entered into with monolist.
 */
package doit.framework.controller;

import java.lang.reflect.Method;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;

import doit.framework.util.AnnotationDWR;
import doit.framework.util.Constants;
import doit.framework.util.SharedMethods;

/**
 * <pre>
 * system      : 공통
 * menu        : 공통
 * source      : BaseDWR.java
 * description : 공통 DWR 부모 클래스
 * </pre> 
 * @version
 * <pre> 
 * 1.0	2008-01-11  monolist  최초 작성
 * 1.1	
 * </pre>
 */

@SuppressWarnings("unchecked")

public class BaseDWR {
	@Autowired
	private AnnotationDWR annotationDWR;
	
	/**
	 * DWR 호출 공통 메소드
	 * @param input Map
	 * @param beanId 서버측 Bean id
	 * @param methodName 서버측 Bean method
	 * @return
	 * @throws Exception
	 */
	public Object handleMap( Map input, String beanId, String methodName) throws Exception
	{
		
		if( input != null)
			SharedMethods.bind( input);
		
		Object object = (Object)ContextLoader.getCurrentWebApplicationContext().getBean(beanId);
		
		if ( object != null && 
			 methodName != null && 
			 !methodName.equals(Constants.VL_EMPTY) && 
			 SharedMethods.validateMethod(object.getClass(), methodName) ) {
				
				Method method = object.getClass().getMethod( methodName, Map.class);
				String className = SharedMethods.getClassName( object.getClass());
				annotationDWR.sessionChecker( input, SharedMethods.getRequest(), SharedMethods.getResponse(), className);
				annotationDWR.annotation( input, SharedMethods.getRequest(), SharedMethods.getResponse(), className);
				
				return method.invoke( object, input);
		}
		
		return null;
	}
	
	/**
	 * DWR 호출 공통 메소드
	 * @param input Map 배열
	 * @param beanId 서버측 Bean id
	 * @param methodName 서버측 Bean method
	 * @return
	 * @throws Exception
	 */
	public Object handleMapArray( Map[] inputs, String beanId, String methodName) throws Exception
	{
		if( inputs != null)
			SharedMethods.bind( inputs);
		
		Object object = (Object)ContextLoader.getCurrentWebApplicationContext().getBean(beanId);
		
		if ( object != null && 
			 methodName != null && 
			 !methodName.equals(Constants.VL_EMPTY) && 
			 SharedMethods.validateMethod(object.getClass(), methodName) ) {
			
			Method method = object.getClass().getMethod( methodName, Map[].class);
            String className = SharedMethods.getClassName( object.getClass());
            annotationDWR.sessionChecker( inputs, SharedMethods.getRequest(), SharedMethods.getResponse(), className);
			annotationDWR.annotation( inputs, SharedMethods.getRequest(), SharedMethods.getResponse(), className);
			
			return method.invoke( object, (Object)inputs);
		}
		
		return null;
	}
}
